package puzzle;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;


public class rompecabeza extends JPanel{
	
	private static final long serialVersionUID = 1L;
	private JButton btn1;
	private JButton btn2;
	private JButton btn3;
	private JButton btn4;
	private JButton btn5;
	private JButton btn6;
	private JButton btn7;
	private JButton btn8;
	private JButton btn9;
	private GridLayout layout;
	

	public rompecabeza() {

		super();
		layout = new GridLayout(3, 3, 3, 3);
		setLayout(layout);

		btn1 = new JButton((new ImageIcon(getClass().getResource("/8.jpg"))));
		btn2 = new JButton((new ImageIcon(getClass().getResource("/2.jpg"))));
		btn3 = new JButton((new ImageIcon(getClass().getResource("/7.jpg"))));
		btn4 = new JButton((new ImageIcon(getClass().getResource("/4.jpg"))));
		btn5 = new JButton((new ImageIcon(getClass().getResource("/3.jpg"))));
		btn6 = new JButton((new ImageIcon(getClass().getResource("/6.jpg"))));
		btn7 = new JButton((new ImageIcon(getClass().getResource("/1.jpg"))));
		btn8 = new JButton((new ImageIcon(getClass().getResource("/5.jpg"))));
		btn9 = new JButton("Boton 9");

		this.add(btn1);
		this.add(btn2);
		this.add(btn3);
		this.add(btn4);
		this.add(btn5);
		this.add(btn6);
		this.add(btn7);
		this.add(btn8);
		this.add(btn9);

		btn9.setVisible(false);

		btn1.addActionListener(new ActionListener(){

			public void actionPerformed (ActionEvent m){

				if(btn2.isVisible()==false){
					btn2.setIcon(btn1.getIcon());
					btn2.setText(btn1.getText());
					btn1.setVisible(false);
					btn2.setVisible(true);
				}

				if(btn4.isVisible()==false){
					btn4.setIcon(btn1.getIcon());	
					btn4.setText(btn1.getText());
					btn1.setVisible(false);
					btn4.setVisible(true);
				}
			}});

		btn2.addActionListener(new ActionListener(){

			public void actionPerformed (ActionEvent m){

				if(btn1.isVisible()==false){
					btn1.setIcon(btn2.getIcon());		
					btn1.setText(btn2.getText());
					btn2.setVisible(false);
					btn1.setVisible(true);
				}

				if(btn3.isVisible()==false){
					btn3.setIcon(btn2.getIcon());
					btn3.setText(btn2.getText());
					btn2.setVisible(false);
					btn3.setVisible(true);
				}

				if(btn5.isVisible()==false){
					btn5.setIcon(btn2.getIcon());
					btn5.setText(btn2.getText());
					btn2.setVisible(false);
					btn5.setVisible(true);
				}
			}});	

		btn3.addActionListener(new ActionListener(){

			public void actionPerformed (ActionEvent m){

				if(btn6.isVisible()==false){

					btn6.setIcon(btn3.getIcon());	
					btn6.setText(btn3.getText());
					btn3.setVisible(false);
					btn6.setVisible(true);
				}

				if(btn2.isVisible()==false){

					btn2.setIcon(btn3.getIcon());
					btn2.setText(btn3.getText());
					btn3.setVisible(false);
					btn2.setVisible(true);
				}
			}});

		btn4.addActionListener(new ActionListener(){

			public void actionPerformed (ActionEvent m){

				if(btn1.isVisible()==false){

					btn1.setIcon(btn4.getIcon());	
					btn1.setText(btn4.getText());
					btn4.setVisible(false);
					btn1.setVisible(true);
				}

				if(btn5.isVisible()==false){

					btn5.setIcon(btn4.getIcon());		
					btn5.setText(btn4.getText());
					btn4.setVisible(false);
					btn5.setVisible(true);
				}

				if(btn7.isVisible()==false){

					btn7.setIcon(btn4.getIcon());
					btn7.setText(btn4.getText());
					btn4.setVisible(false);
					btn7.setVisible(true);
				}

			}});

		btn5.addActionListener(new ActionListener(){

			public void actionPerformed (ActionEvent m){

				if(btn6.isVisible()==false){

					btn6.setIcon(btn5.getIcon());	
					btn6.setText(btn5.getText());
					btn5.setVisible(false);
					btn6.setVisible(true);
				}

				if(btn8.isVisible()==false){

					btn8.setIcon(btn5.getIcon());
					btn8.setText(btn5.getText());
					btn5.setVisible(false);
					btn8.setVisible(true);
				}

				if(btn2.isVisible()==false){

					btn2.setIcon(btn5.getIcon());
					btn2.setText(btn5.getText());
					btn5.setVisible(false);
					btn2.setVisible(true);
				}

				if(btn4.isVisible()==false){

					btn4.setIcon(btn5.getIcon());
					btn4.setText(btn5.getText());
					btn5.setVisible(false);
					btn4.setVisible(true);
				}
			}});	

		btn6.addActionListener(new ActionListener(){

			public void actionPerformed (ActionEvent m){

				if(btn9.isVisible()==false){

					btn9.setIcon(btn6.getIcon());	
					btn9.setText(btn6.getText());
					btn6.setVisible(false);
					btn9.setVisible(true);
				}

				if(btn5.isVisible()==false){

					btn5.setIcon(btn6.getIcon());	
					btn5.setText(btn6.getText());
					btn6.setVisible(false);
					btn5.setVisible(true);
				}

				if(btn3.isVisible()==false){

					btn3.setIcon(btn6.getIcon());
					btn3.setText(btn6.getText());
					btn6.setVisible(false);
					btn3.setVisible(true);
				}
			}});

		btn7.addActionListener(new ActionListener(){

			public void actionPerformed (ActionEvent m){

				if(btn4.isVisible()==false){

					btn4.setIcon(btn7.getIcon());	
					btn4.setText(btn7.getText());
					btn7.setVisible(false);
					btn4.setVisible(true);
				}

				if(btn8.isVisible()==false){

					btn8.setIcon(btn7.getIcon());	
					btn8.setText(btn7.getText());
					btn7.setVisible(false);
					btn8.setVisible(true);
					}
			}});

		btn8.addActionListener(new ActionListener(){

			public void actionPerformed (ActionEvent m){

				if(btn5.isVisible()==false){

					btn5.setIcon(btn8.getIcon());
					btn5.setText(btn8.getText());
					btn8.setVisible(false);
					btn5.setVisible(true);
				}

				if(btn7.isVisible()==false){

					btn7.setIcon(btn8.getIcon());
					btn7.setText(btn8.getText());
					btn8.setVisible(false);
					btn7.setVisible(true);
				}

				if(btn9.isVisible()==false){

					btn9.setIcon(btn8.getIcon());
					btn9.setText(btn8.getText());
					btn8.setVisible(false);
					btn9.setVisible(true);
				}
			}});

		btn9.addActionListener(new ActionListener(){

			public void actionPerformed (ActionEvent m){

				if(btn6.isVisible()==false){

					btn6.setIcon(btn9.getIcon());	
					btn6.setText(btn9.getText());
					btn9.setVisible(false);
					btn6.setVisible(true);
				}

				if(btn8.isVisible()==false){

					btn8.setIcon(btn9.getIcon());	
					btn8.setText(btn9.getText());
					btn9.setVisible(false);
					btn8.setVisible(true);
				}
			}
		});
		}
	
}